/*
 * #%L
 * SwiftUICommonUtilities
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2011 - 2012 Linkwithweb
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */
/**
 * 
 */
package com.linkwithweb.products.swiftui.dataobject;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.linkwithweb.products.swiftui.ui.model.RequestCondition;
import com.linkwithweb.products.swiftui.ui.model.RicoColumnSpec;
import com.linkwithweb.products.swiftui.ui.model.RicoTable;
import com.linkwithweb.products.swiftui.utils.SystemConstants;
import com.linkwithweb.products.swiftui.utils.SystemStringUtils;

/**
 * DROP TABLE IF EXISTS `automata`.`frm_text_report`;
 * CREATE TABLE `automata`.`frm_text_report` (
 * `REPORT_KEY` int(10) unsigned NOT NULL AUTO_INCREMENT,
 * `REPORT_ID` varchar(45) DEFAULT NULL,
 * `REPORT_NAME` varchar(45) DEFAULT NULL,
 * `TABLE_ID` varchar(45) DEFAULT NULL,
 * `REPORT_DESCRIPTION` varchar(45) DEFAULT NULL,
 * `REPORT_HEADING` varchar(45) DEFAULT NULL,
 * `REPORT_TYPE` varchar(45) DEFAULT NULL,
 * `FROZEN_COLUMN_COUNT` varchar(45) DEFAULT NULL,
 * `REPORT_DEFAULT_CSS` varchar(45) DEFAULT NULL,
 * `REPORT_DEFAULT_FILTER_STRING` varchar(45) DEFAULT NULL,
 * `REPORT_SEARCH_ACTION` varchar(45) DEFAULT NULL,
 * `IS_ACTIVE` tinyint(1) NOT NULL,
 * `IDENTIFIER` varchar(45) DEFAULT NULL,
 * `IDENTIFIER_ROLE` varchar(45) DEFAULT NULL,
 * `CREATED_BY` int(10) unsigned NOT NULL,
 * `CREATED_DATE` datetime NOT NULL,
 * `MODIFIED_BY` int(10) unsigned NOT NULL,
 * `MODIFIED_DATE` datetime NOT NULL,
 * PRIMARY KEY (`REPORT_KEY`)
 * ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
 * 
 * @author ashwin kumar
 * 
 */
public class FrameworkTextReport extends BaseDataObject {
	/**
	 * 
	 */
	private static final int MAX_COL_WIDTH = 250;
	/**
	 * 
	 */
	private static final int MIN_DISPLAY_WIDTH = 140;
	private long reportKey = -1;
	private String reportId = "";
	private String reportName = "";
	private String tableId = "";
	private String reportDescription = "";
	private String reportHeading = "";
	private String reportType = "";
	private int frozenColumnCount = 1;
	private String reportDefaultCss = "";
	private String reportDefaultFilterString = "";

	private boolean isEditable = true;
	private boolean isMenuDisplayable = true;

	private List<FrameworkReportColumn> columns = new ArrayList<FrameworkReportColumn>();
	private FrameworkTable frameworkTable = null;
	private RicoTable ricoTable = null;
	private List<RequestCondition> conditions = new ArrayList<RequestCondition>();
	private String orderByCluase = "";

	/**
	 * @return the reportKey
	 */
	public long getReportKey() {
		return reportKey;
	}

	/**
	 * @param reportKey
	 *            the reportKey to set
	 */
	public void setReportKey(long reportKey) {
		this.reportKey = reportKey;
	}

	/**
	 * @return the reportId
	 */
	public String getReportId() {
		return reportId;
	}

	/**
	 * @param reportId
	 *            the reportId to set
	 */
	public void setReportId(String reportId) {
		this.reportId = reportId;
	}

	/**
	 * @return the reportName
	 */
	public String getReportName() {
		return reportName;
	}

	/**
	 * @param reportName
	 *            the reportName to set
	 */
	public void setReportName(String reportName) {
		this.reportName = reportName;
	}

	/**
	 * @return the tableId
	 */
	public String getTableId() {
		return tableId;
	}

	/**
	 * @param tableId
	 *            the tableId to set
	 */
	public void setTableId(String tableId) {
		this.tableId = tableId;
	}

	/**
	 * @return the reportDescription
	 */
	public String getReportDescription() {
		return reportDescription;
	}

	/**
	 * @param reportDescription
	 *            the reportDescription to set
	 */
	public void setReportDescription(String reportDescription) {
		this.reportDescription = reportDescription;
	}

	/**
	 * @return the reportHeading
	 */
	public String getReportHeading() {
		return reportHeading;
	}

	/**
	 * @param reportHeading
	 *            the reportHeading to set
	 */
	public void setReportHeading(String reportHeading) {
		this.reportHeading = reportHeading;
	}

	/**
	 * @return the reportType
	 */
	public String getReportType() {
		return reportType;
	}

	/**
	 * @param reportType
	 *            the reportType to set
	 */
	public void setReportType(String reportType) {
		this.reportType = reportType;
	}

	/**
	 * @return the frozenColumnCount
	 */
	public int getFrozenColumnCount() {
		return frozenColumnCount;
	}

	/**
	 * @param frozenColumnCount
	 *            the frozenColumnCount to set
	 */
	public void setFrozenColumnCount(int frozenColumnCount) {
		this.frozenColumnCount = frozenColumnCount;
	}

	/**
	 * @return the reportDefaultCss
	 */
	public String getReportDefaultCss() {
		return reportDefaultCss;
	}

	/**
	 * @param reportDefaultCss
	 *            the reportDefaultCss to set
	 */
	public void setReportDefaultCss(String reportDefaultCss) {
		this.reportDefaultCss = reportDefaultCss;
	}

	/**
	 * @return the reportDefaultFilterString
	 */
	public String getReportDefaultFilterString() {
		return reportDefaultFilterString;
	}

	/**
	 * @param reportDefaultFilterString
	 *            the reportDefaultFilterString to set
	 */
	public void setReportDefaultFilterString(String reportDefaultFilterString) {
		this.reportDefaultFilterString = reportDefaultFilterString;
	}

	/**
	 * @return the columns
	 */
	public List<FrameworkReportColumn> getColumns() {
		return columns;
	}

	/**
	 * @param columns
	 *            the columns to set
	 */
	public void setColumns(List<FrameworkReportColumn> columns) {
		this.columns = columns;
	}

	public FrameworkReportColumn getColumnByName(String columnName) {
		for (FrameworkReportColumn column : columns) {
			if (StringUtils.equalsIgnoreCase(column.getColumnName(), columnName)) {
				return column;
			}
		}
		return null;
	}

	/**
	 * Makes sure we have one instance of Report Column(Behaves like set)
	 * 
	 * @param reportColumn
	 */
	public void addColumn(FrameworkReportColumn reportColumn) {
		int objectIndex = this.columns.indexOf(reportColumn);
		reportColumn.setDisplayWidth(this.columns.get(objectIndex).getDisplayWidth());
		if (objectIndex == -1) {
			this.columns.remove(reportColumn);
			this.columns.add(reportColumn);
		} else {
			this.columns.set(objectIndex, reportColumn);
		}

	}

	/**
	 * @return the frameworkTable
	 */
	public FrameworkTable getFrameworkTable() {
		return frameworkTable;
	}

	/**
	 * @param frameworkTable
	 *            the frameworkTable to set
	 */
	public void setFrameworkTable(FrameworkTable frameworkTable) {
		this.frameworkTable = frameworkTable;
	}

	/**
	 * @return the ricoTable
	 */
	public RicoTable getRicoTable() {
		if (ricoTable == null) {
			generateRicoTable(this);
		}
		return ricoTable;
	}

	/**
	 * @param ricoTable
	 *            the ricoTable to set
	 */
	public void setRicoTable(RicoTable ricoTable) {
		this.ricoTable = ricoTable;
	}

	/**
	 * @return the conditions
	 */
	public List<RequestCondition> getConditions() {
		return conditions;
	}

	/**
	 * @param conditions
	 *            the conditions to set
	 */
	public void setConditions(List<RequestCondition> conditions) {
		this.conditions = conditions;
	}

	/**
	 * @return the orderByCluase
	 */
	public String getOrderByCluase() {
		return orderByCluase;
	}

	/**
	 * @param orderByCluase
	 *            the orderByCluase to set
	 */
	public void setOrderByCluase(String orderByCluase) {
		this.orderByCluase = orderByCluase;
	}

	/**
	 * @return the isEditable
	 */
	public boolean isEditable() {
		return isEditable;
	}

	/**
	 * @param isEditable
	 *            the isEditable to set
	 */
	public void setEditable(boolean isEditable) {
		this.isEditable = isEditable;
	}

	/**
	 * @return the isMenuDisplayable
	 */
	public boolean isMenuDisplayable() {
		return isMenuDisplayable;
	}

	/**
	 * @param isMenuDisplayable
	 *            the isMenuDisplayable to set
	 */
	public void setMenuDisplayable(boolean isMenuDisplayable) {
		this.isMenuDisplayable = isMenuDisplayable;
	}

	/**
	 * @param reportComponent
	 */
	protected RicoTable generateRicoTable(FrameworkTextReport reportObject) {
		ricoTable = new RicoTable();
		ricoTable.setEditable(reportObject.isEditable());
		ricoTable.setMenuDisplayable(reportObject.isMenuDisplayable());
		RicoColumnSpec columnSpec = null;
		int i = 0;
		int columnIndex = 0;
		for (FrameworkReportColumn reportColumn : reportObject.getColumns()) {
			if (!reportColumn.isDisplayable()) {
				continue;
			}

			if (StringUtils.isNotBlank(reportColumn.getColumnGroup())
					&& !ricoTable.getPanels().contains(reportColumn.getColumnGroup().replaceAll("/", "-"))) {
				ricoTable.getPanels().add(reportColumn.getColumnGroup().replaceAll("/", "-"));
				if (ricoTable.getPanels().size() > 1 && ricoTable.getPanels().size() < 3) {
					ricoTable.setFrozenColumns(columnIndex - 1);
				}
			}

			columnSpec = new RicoColumnSpec();
			columnSpec.setFieldName(reportColumn.getColumnName());

			if (ricoTable.getPanels().size() > 0 && StringUtils.isNotBlank(reportColumn.getColumnGroup())) {
				columnSpec.setPanelIdx(ricoTable.getPanels()
						.indexOf(reportColumn.getColumnGroup().replaceAll("/", "-")) + 1);
			}
			if (columnIndex == 0) {
				columnSpec.setPanelIdx(0);
			}

			if (columnSpec.getPanelIdx() == -1) {
				columnSpec.setPanelIdx(1);
			}

			columnSpec.setReadOnly(false);
			columnSpec.setColName(reportColumn.getColumnName());

			columnSpec.setHdg(reportColumn.getColumnLabel().replaceAll("\"", ""));
			if (StringUtils.equalsIgnoreCase(reportColumn.getColumnName(), reportColumn.getColumnLabel())) {
				columnSpec.setHdg(SystemStringUtils.toTitleCase(reportColumn.getColumnLabel()));
				/*
				 * columnSpec.setFieldName(SystemStringUtils
				 * .toTitleCase(reportColumn.getColumnLabel()));
				 */
			}
			columnIndex = columnIndex + 1;
			columnSpec.setSelectCtl(null);
			// Presently we use Rico Default FiiterUI Types
			if (StringUtils.isNotBlank(reportColumn.getFilterType())) {
				columnSpec.setFilterUI(reportColumn.getFilterType());
			}
			columnSpec.setEntryType(SystemConstants.RicoEntryTypes.B);
			if (reportColumn.getDisplayWidth() > 50) {
				columnSpec.setEntryType(SystemConstants.RicoEntryTypes.TA);
				columnSpec.setTxtAreaCols("60");
				columnSpec.setTxtAreaRows("4");
			}

			if (StringUtils.contains(reportColumn.getColumnType(), SystemConstants.ColumnTypes.TINYINT)
					|| StringUtils.equalsIgnoreCase(reportColumn.getColumnType(), SystemConstants.ColumnTypes.BOOLEAN)
					|| StringUtils.equalsIgnoreCase(reportColumn.getColumnType(), SystemConstants.ColumnTypes.BIT)) {
				columnSpec.setEntryType(SystemConstants.RicoEntryTypes.CH);
			} else if (StringUtils.contains(reportColumn.getColumnType(), SystemConstants.ColumnTypes.INTEGER)) {
				columnSpec.setEntryType(SystemConstants.RicoEntryTypes.I);
				columnSpec.setType("number");
			} else if (StringUtils.contains(reportColumn.getColumnType(), SystemConstants.ColumnTypes.LOV)) {
				columnSpec.setEntryType(SystemConstants.RicoEntryTypes.S);
				columnSpec.setFilterUI(SystemConstants.RicoFilterTypes.S);
			} else if (StringUtils.contains(reportColumn.getColumnType(), SystemConstants.ColumnTypes.DATETIME)
					|| StringUtils.contains(reportColumn.getColumnType(), SystemConstants.ColumnTypes.DATE)
					|| StringUtils.contains(reportColumn.getColumnType(), SystemConstants.ColumnTypes.TIME)) {
				columnSpec.setColData("2009-11-09");
				columnSpec.setEntryType(SystemConstants.RicoEntryTypes.DT);
				columnSpec.setSelectCtl("Cal");
				columnSpec.setType("date");
			} else {
				columnSpec.setColData("");
			}

			columnSpec.setLength(reportColumn.getDisplayWidth());

			/**
			 * <#if columnInfo.columnType = 'DATE' || columnInfo.columnType =
			 * 'DATETIME' || columnInfo.columnType = 'INGRESDATE'> EntryType:
			 * "DT", type: "date", SelectCtl: "Cal", <#else>
			 */
			// columnSpec.setInsertOnly(true);
			columnSpec.setWidth(reportColumn.getDisplayWidth());
			if (reportColumn.getDisplayWidth() < MIN_DISPLAY_WIDTH) {
				columnSpec.setWidth(MIN_DISPLAY_WIDTH);
			}
			if (reportColumn.getDisplayWidth() > MAX_COL_WIDTH) {
				columnSpec.setWidth(MAX_COL_WIDTH);
			}

			columnSpec.setNullable(true);
			columnSpec.setWriteable(true);

			if (getFrameworkTable().getPrimaryKeyColumns() != null
					&& getFrameworkTable().getPrimaryKeyColumns().contains(reportColumn.getColumnName())) {
				columnSpec.setKey(true);
				//columnSpec.setReadOnly(true);
			} else {
				columnSpec.setKey(false);
			}

			// columnSpec.setLength(20);
			if (StringUtils.isNotBlank(reportColumn.getColumnType())
					&& StringUtils.equalsIgnoreCase(reportColumn.getColumnType(), SystemConstants.HYPERLINK)
					&& StringUtils.isNotBlank(reportColumn.getHyperlinkText())) {
				columnSpec.setControl("new Rico.TableColumn.link(\'" + reportColumn.getHyperlinkText() + "/{" + i
						+ "}\',\'_blank\')");
			} else if (StringUtils.isNotBlank(reportColumn.getColumnType())
					&& StringUtils.equalsIgnoreCase(reportColumn.getColumnType(), SystemConstants.HYPERLINK)) {
				columnSpec.setControl("new Rico.TableColumn.link(\'http://{" + i + "}\',\'_blank\')");
			} else if (StringUtils.isNotBlank(reportColumn.getColumnType())
					&& StringUtils.equalsIgnoreCase(reportColumn.getColumnType(), SystemConstants.IMAGEURL)) {
				columnSpec.setControl("new Rico.TableColumn.image()");
			} else if (StringUtils.isNotBlank(reportColumn.getColumnType())
					&& (StringUtils.equalsIgnoreCase(reportColumn.getColumnType(), SystemConstants.ColumnTypes.TINYINT)
							|| StringUtils.equalsIgnoreCase(reportColumn.getColumnType(),
									SystemConstants.ColumnTypes.BOOLEAN) || StringUtils.equalsIgnoreCase(reportColumn
							.getColumnType(), SystemConstants.ColumnTypes.BIT))) {
				columnSpec.setControl("new Rico.TableColumn.checkbox('true','false','true',true)");
			} else if (StringUtils.isNotBlank(reportColumn.getColumnType())
					&& (StringUtils.equalsIgnoreCase(reportColumn.getColumnType(), SystemConstants.ColumnTypes.BIT))) {
				columnSpec.setControl("new Rico.TableColumn.checkbox('1','0','1',true)");
			} else {
				columnSpec.setControl(null);
			}
			ricoTable.addRicoColumn(columnSpec);
			i++;
		}

		if (ricoTable.getPanels().size() < 2) {
			if (ricoTable.getPanels().size() == 0) {
				ricoTable.getPanels().add(reportObject.getReportName());
				ricoTable.getPanels().add(reportObject.getReportName());
			} else {
				ricoTable.getPanels().add(reportObject.getReportName());
			}
		}

		return ricoTable;
	}
}
